from ctypes import CDLL,byref,create_string_buffer
from ctypes import cdll
import json
import os
import sys

def recog_dll_split_img(string):
    split_img_dll_path = r'C:\Users\vanlance\Desktop\RecogDLL_split_img_python\x64\Release\RecogDLL_split_img.dll'
    split_img_dll = CDLL(split_img_dll_path)
    input_string_buffer = create_string_buffer(string.encode('GB2312'), 65536)
    output_string_buffer = create_string_buffer(65536)
    split_img_dll.split_img(byref(input_string_buffer), byref(output_string_buffer))
    output_json = json.loads(output_string_buffer.value.decode('GB2312'))
    return output_json


def recog_dll_extract_form(string):
    extract_form_dll_path = r'D:\ninbo\RecogDLL_extract_form_python\x64\Release\RecogDLL_extract_form.dll'
    extract_form_dll = CDLL(extract_form_dll_path)
    input_string_buffer = create_string_buffer(string.encode('GB2312'), 65536)
    output_string_buffer = create_string_buffer(65536)
    extract_form_dll.extract_form(byref(input_string_buffer), byref(output_string_buffer))
    output_json = json.loads(output_string_buffer.value.decode('GB2312'))
    return output_json


def recog_dll_optimize_recog(img_path_dir):
    optimize_recog_dll_path = '/home/swls/work_dir/git/socket_recog/build/src/libextract_form.so'
    optimize_recog_dll = cdll.LoadLibrary(optimize_recog_dll_path)
    model_path = "/home/swls/work_dir/git/socket_recog/train_model/saved_model/output.pb"
    so_path = '/home/swls/work_dir/github/tensorflow-1.13.0-rc2/bazel-bin/tensorflow/cc/socket_recog/recoglib.so'
    input_model_path_buffer0 = create_string_buffer(model_path.encode('utf-8'), 65536)
    input_so_path_buffer0 = create_string_buffer(so_path.encode('utf-8'), 65536)

    out = optimize_recog_dll.initModel(input_model_path_buffer0, input_so_path_buffer0)
    print('out: ', out)

    for idx, p in enumerate(os.listdir(img_path_dir)):
        # if idx > 10:
        #     break
        # if (p[-3:] == 'png'):
        path = os.path.join(img_path_dir, p)
        input_string_buffer = create_string_buffer(path.encode('utf-8'), 65536)
        out2 = optimize_recog_dll.imgRec(byref(input_string_buffer))
        print('out2: ', out2)

    # output_serial = output_string_buffer.value.decode('GB2312')
    # return output_serial


if __name__ == "__main__":
    # d ={
    #     "code":0,
    #     "image_list":[
    #         "D:\\javaworkspace\\docdistinguish\\target\\classes\\static\\D.3.1.2017-17-2\\我.jpg",
    #         # "D:\\javaworkspace\\docdistinguish\\target\\classes\\static\\D.3.1.2017-17-2\\D.3.1.2017-17-2_2.jpg",
    #         # "D:\\javaworkspace\\docdistinguish\\target\\classes\\static\\D.3.1.2017-17-2\\D.3.1.2017-17-2_3.jpg",
    #         # "D:\\javaworkspace\\docdistinguish\\target\\classes\\static\\D.3.1.2017-17-2\\D.3.1.2017-17-2_4.jpg",
    #         "D:\\javaworkspace\\docdistinguish\\target\\classes\\static\\D.3.1.2017-17-2\\我我.jpg",
    #         "D:\\javaworkspace\\docdistinguish\\target\\classes\\static\\D.3.1.2017-17-2\\D.3.2011-3-3_2.jpg",
    #     ]
    # }
    
    # out = recog_dll_split_img(json.dumps(d, ensure_ascii=False))
    # print(out)

    # img_path_dir = '/home/swls/work_dir/git/socket_recog/train_model/dataset/train/1'
    # recog_dll_optimize_recog(img_path_dir)
    # path = r'D:\javaworkspace\docdistinguish\target\classes\static\D.3.1.2017-17-2\D.3.2011-3-3_2_left.jpg'
	
	
    # path = r'C:\Users\vanlance\Desktop\2\002.jpg'
    # out = recog_dll_extract_form(path)
    # print(out)

    
    # path = r'D:\javaworkspace\docdistinguish\target\classes\static\tempJson\1VEoFA8m12.json'
    # out = recog_dll_optimize_recog(path)
    # print(out)
    string = sys.argv[1]
    # string2 = sys.argv[2]
    optimize_recog_dll_path = r'C:\Users\swls\Documents\Tencent Files\360697611\FileRecv\libshort_ocr.dll'
    optimize_recog_dll = CDLL(optimize_recog_dll_path)
    # input_string_buffer = create_string_buffer(string.encode('utf-8'), 65536)
    # # input_string_buffer2 = create_string_buffer(string2.encode('utf-8'), 65536)
    # output_string_buffer = create_string_buffer(65536)
    # optimize_recog_dll.test(byref(input_string_buffer), byref(output_string_buffer))
    # output_serial = output_string_buffer.value.decode('utf-8')
    # print(output_serial, ' ', 'hhh') 

    pass